class Solution {
public:
    int maxTurbulenceSize(vector<int>& arr) 
    {
        int ret = 1;
        int left = 0, right = 0;

        while (right < arr.size() - 1)
        {
            if (left == right)
            {
                if(arr[left] == arr[left + 1])
                {
                    left++;
                }
                right++;
            }
            else
            {
                if (arr[right - 1] < arr[right] && arr[right] > arr[right + 1])
                {
                    right++;
                }
                else if (arr[right - 1] > arr[right] && arr[right] < arr[right + 1])
                {
                    right++;
                }
                else
                {
                    left = right;
                }

            }
            ret = max(ret, right - left + 1);
        }
        return ret;

    }
};